package com.itboye.textileexpres.login;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.UserBean;
import com.itboye.textileexpes.presenter.AppUpdatePresenter;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.logincontroller.LoginController;
import com.itboye.textileexpres.logincontroller.LoginedState;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexptes.config.GlobalConfig;

/**
 * 
 * @author 验证码加密码（注册/修改密码）
 * 
 */
public class LoginReginPwasswroldActibity extends BaseActivity implements
		Observer, TextWatcher, OnClickListener {

	String phone;
	String number;
	String code;
	TextView phone_word;
	EditText pass_word;
	EditText confirm_pass_word;
	TextView tvPhone;
	MainPresenter mainPresenter;
	ImageView img_other;
	private String strNum;
	TextView txt_save;
	TextView title;
	boolean strpwss_wold = false;
	ImageView back;
	String type;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_loginregoin;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		txt_save.setVisibility(View.VISIBLE);
		txt_save.setEnabled(false);
		mainPresenter = new MainPresenter(this);
		title.setText("设置密码");
		txt_save.setText("完成");
		phone = getIntent().getStringExtra("phone");
		number = getIntent().getStringExtra("number");
		code = getIntent().getStringExtra("code");
		type = getIntent().getStringExtra("type");
		strNum = number + phone;
		confirm_pass_word.addTextChangedListener(this);
		Log.e("hu.....loginregin", phone);
		tvPhone.setText("请设置密码，之后你可以使用手机号\n+86" + phone + "+密码进行登录");
		phone_word.setText(phone);
		strpwss_wold = confirm_pass_word.getText().equals(
				pass_word.getText().toString());
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(arg1);
		if (entity != null) {
			if (entity.getEventType() == MainPresenter.RegisterSuccessEvent) {
				toast("" + entity.getData());
				// LoginSuccess(entity);
				try {
					closeProgressDialog();
				} catch (Exception e) {
					// TODO: handle exception
				}
				finish();
			} else if (entity.getEventType() == MainPresenter.RegisterFailureEvent) {
				toast("" + entity.getData());
			}
			if (entity.getEventTag() == MainPresenter.UpdatePwdByPhoneSuccessEvent) {
				toast("" + entity.getData());
				finish();
			} else if (entity.getEventTag() == MainPresenter.UpdatePwdByPhoneFailureEvent) {
				toast("" + entity.getData());
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		if (confirm_pass_word.getText().toString().length() > 0) {
			txt_save.setTextColor(getResources().getColor(R.color.white));
			txt_save.setEnabled(true);
		} else {
			txt_save.setTextColor(getResources().getColor(R.color.gray));
			txt_save.setEnabled(false);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.txt_save:
			if (confirm_pass_word.getText().toString().length() > 0
					&& pass_word.getText().toString().length() > 0) {
			} else {
				toast("密码不能为空");
				return;
			}
			if ((confirm_pass_word.getText() + "").equals(pass_word.getText()
					+ "")) {
				if ("register".equals(type)) {
					mainPresenter.register(code, number, phone,
							confirm_pass_word.getText() + "", "0");
				} else {
					mainPresenter.updatePasswordByTelephone(code, number,
							confirm_pass_word.getText() + "", phone);
				}

				// Log.e("hu", code + "," + number + "," + phone + ","
				// + confirm_pass_word.getText() + "" + type);
			} else {
				toast("两次密码不一致");
				return;
			}
			break;
		case R.id.back:
			this.finish();
			break;

		default:
			break;
		}
	}
}
